import router from "./router";
import NProgress from "nprogress";
import 'nprogress/nprogress.css'// Progress 进度条样式
// import { ElMessage } from 'element-plus'
import useUserStore from './store/modules/user'
import usePermissionStore from "./store/modules/permission"
import useAppStore from "./store/modules/app"
import { storeToRefs } from  "pinia"


const whiteList = ['/login'] // 不重定向白名单
let addDynamicRoutes = false

router.beforeEach((to, from, next)=>{
    NProgress.start()
    const userStore = useUserStore()
    // const permissionStore = usePermissionStore()
    // const appStore = useAppStore()
    let { authToken, roles }  = storeToRefs(userStore)
    if(authToken.value || sessionStorage.getItem('authToken')){ // 已登录过
        if(to.path==='/login'){
            next({path: '/home'})
            // if current page is dashboard will not trigger afterEach hook, so manually handle it
            NProgress.done() 
        } else {
            next()
        }   
    }else{
        if(whiteList.indexOf(to.path)!=-1){
            next()
        }else{
            next('/login')
            NProgress.done()
        }
    }
})

router.afterEach(() => {
    NProgress.done() // 结束Progress
  })

